﻿#include "UIWidget.hpp"
#include "Trigger.hpp"
#include "raceScene.hpp"

//Dialog to enter the password
class PasswordDlg : EventHandler
{
	PasswordDlg()
	{
		@WindowPassword = Window();
		WindowPassword.window.LoadLayout("Password.layout");
		WindowPassword.window.SetVisible(false);
		WindowPassword.window.SetPos(0.2f,0.1f);

		@EditBoxPassword = UiSystem_I.GetEditBox("PasswordWindow/PasswordInput");
		EditBoxPassword.setValidationString("[A-Za-z0-9]*");
		EditBoxPassword.setTextMasked(true);

		@ButtonEnter = UiSystem_I.GetButton("PasswordWindow/EnterButton");
		ButtonEnter.clickSlot.SetHandler(this,"OnButtonEnter");

		@ButtonCancel = UiSystem_I.GetButton("PasswordWindow/CancelButton");
		ButtonCancel.clickSlot.SetHandler(this,"OnButtonCancel");

	}

	void OnButtonEnter()
	{
		if(true/*if the password is correct*/)
		{
			g_TitleUI.HideAll();

			if(!raceSceneManager_I.StartScene("SimpleScene"))
			{
				cout<<"无此赛道"<<endl;
			}
		}
		else
		{
			WindowPassword.window.SetText("Though my throat's painful,password is wrong");
			EditBoxPassword.SetText("");
		}

	}

	void OnButtonCancel()
	{
		Show(false);
		WindowPassword.window.SetText("Enter Password");

	}

	void Show(bool val)
	{
		WindowPassword.window.SetVisible(val);
	}

	bool isShow()
	{
		return WindowPassword.window.IsVisible();
	}

	Window@				WindowPassword;
	WidgetEditBox@		EditBoxPassword;
	WidgetButton@		ButtonEnter;
	WidgetButton@		ButtonCancel;
};

//Dialog to Join Room
class JoinRoomDlg : EventHandler
{
	JoinRoomDlg()
	{
		@WindowJoinRoom = Window();
		WindowJoinRoom.window.LoadLayout("JoinRoom.layout");
		WindowJoinRoom.window.SetVisible(false);
		WindowJoinRoom.window.SetPos(0.15f,0.25f);

		@ButtonJoin = UiSystem_I.GetButton("DefaultGUISheet/FrameWindow/JoinButton");
		ButtonJoin.clickSlot.SetHandler(this,"OnButtonJoin");
		@ButtonRefresh = UiSystem_I.GetButton("DefaultGUISheet/FrameWindow/RefreshButton");
		ButtonRefresh.clickSlot.SetHandler(this,"OnButtonRefresh");
		@ButtonCancel = UiSystem_I.GetButton("DefaultGUISheet/FrameWindow/CancelButton");
		ButtonCancel.clickSlot.SetHandler(this,"OnButtonCancel");
		@serverList = UiSystem_I.GetMultilineList("DefaultGUISheet/FrameWindow/MultilineList");

		@passwordDlg = PasswordDlg();
		WindowJoinRoom.window.AddChildWindow(passwordDlg.WindowPassword.window);

	}

	void RefreshList()
	{
		//探测开启的服务器
		NetManager_I.SeekforServer(500);

		uint serverNum = NetManager_I.GetServerCount();

		serverList.ClearItems();
		for(uint i=0; i<serverNum; i++)
		{
			serverList.AddRow(i);
			RoomDesc server;
			NetManager_I.GetRoomList(i,server);
			string ip;
			SysAddrGetString(server.serverAdd,false,ip);
			string port = server.serverAdd.port;

			serverList.SetRowItem(server.hostName,i,0);
			serverList.SetRowItem(ip,i,1);
			serverList.SetRowItem(port,i,2);
			serverList.SetRowItem("Normal",i,3);
			serverList.SetRowItem("Normal",i,4);
		}
	}

	void OnButtonJoin()
	{
		uint serverNum = NetManager_I.GetServerCount();
		if( 0 == serverNum)
		{
			return;
		}
		if(true/*if room has a password*/)
		{

			//passwordDlg.Show(!passwordDlg.isShow());
		}
		string ip;
		serverList.GetCurtSel(ip,1);
		cast<NetClient@>(NetManager_I.net).ConnectToServer(ip);
	}

	void OnButtonCancel()
	{
		NetManager_I.Shutdown();
		Show(false);
	}

	void OnButtonRefresh()
	{
		///
		RefreshList();
	}

	void Show(bool val)
	{
		WindowJoinRoom.window.SetVisible(val);
	}

	bool isShow()
	{
		return WindowJoinRoom.window.IsVisible();
	}

	Window@				WindowJoinRoom;
	WidgetListBox@		ListBoxRoom;
	WidgetButton@		ButtonJoin;
	WidgetButton@		ButtonRefresh;
	WidgetButton@		ButtonCancel;
	PasswordDlg@		passwordDlg;
	WidgetMultilineList@serverList;
};

//创建房间后等待对话框
class CreateWaitDlg : EventHandler
{
	CreateWaitDlg()
	{
		@createWaitWnd = Window();
		createWaitWnd.window.LoadLayout("CreateWait.layout");
		createWaitWnd.window.SetVisible(false);
		createWaitWnd.window.SetPos(0.1f,0.15f);

		@startBtn = UiSystem_I.GetButton("CreateState_Start");
		startBtn.clickSlot.SetHandler(this,"OnButtonStart");

		@refreshBtn = UiSystem_I.GetButton("CreateState_Refresh");
		refreshBtn.clickSlot.SetHandler(this,"OnButtonRefresh");

		@quitBtn = UiSystem_I.GetButton("CreateState_Quit");
		quitBtn.clickSlot.SetHandler(this,"OnButtonCancel");

		@clientList = UiSystem_I.GetMultilineList("Create_Players");
	}

	void RefreshClientList()
	{
		uint clientNums = cast<NetServer@>(NetManager_I.net).PlayerNums();
		
		clientList.ClearItems();

		for (uint i=0; i<clientNums; i++)
		{
			string ip;
			int port = cast<NetServer@>(NetManager_I.net).GetClientFromList(ip,i);
			string portStr = port;

			cout << ip << endl << port << endl;

			clientList.AddRow(i);
			clientList.SetRowItem("Unname",i,0);
			clientList.SetRowItem(ip,i,1);
			clientList.SetRowItem(portStr,i,2);
			
		}
	}

	void OnButtonStart()
	{
		///为客户端分配网络ID
		cast<NetServer@>(NetManager_I.net).SendNetIDtoClient();
		
		///发送创建场景消息
		NetManager_I.net.SendFlag(ID_BEGIN_GAME);

		g_TitleUI.HideAll();

		if(!raceSceneManager_I.StartScene("SimpleScene"))
		{
			cout<<"无此赛道"<<endl;
		}

		cast<NetServer@>(NetManager_I.net).CreateOtherCars();

		Show(false);
	}

	void OnButtonRefresh()
	{
		RefreshClientList();
	}

	void OnButtonCancel()
	{
		NetManager_I.Shutdown();
		Show(false);
	}

	void Show(bool val)
	{
		createWaitWnd.window.SetVisible(val);
	}

	bool isShow()
	{
		return createWaitWnd.window.IsVisible();
	}

	Window@				createWaitWnd;
	WidgetListBox@		playersList;
	WidgetButton@		startBtn;
	WidgetButton@		refreshBtn;
	WidgetButton@		quitBtn;
	WidgetMultilineList@clientList;
};


//Dialog to create Room
class CreateRoomDlg : EventHandler
{
	CreateRoomDlg()
	{
		@WindowCreateRoom = Window();
		WindowCreateRoom.window.LoadLayout("CreateRoom.layout");
		WindowCreateRoom.window.SetVisible(false);
		WindowCreateRoom.window.SetPos(0.1f,0.15f);

		@EditBoxRoomName = UiSystem_I.GetEditBox("RoomInput");
		EditBoxRoomName.setValidationString("[A-Za-z0-9]*");

		@EditBoxPassword = UiSystem_I.GetEditBox("PasswordInput");
		EditBoxPassword.setValidationString("[A-Za-z0-9]*");
		EditBoxPassword.setTextMasked(true);
		@ButtonConfirm = UiSystem_I.GetButton("Confirm");
		ButtonConfirm.clickSlot.SetHandler(this,"OnButtonConfirm");


		@ButtonCancel = UiSystem_I.GetButton("Cancel");
		ButtonCancel.clickSlot.SetHandler(this,"OnButtonCancel");

		@waitDlg = CreateWaitDlg();
		waitDlg.Show(false);

		WindowCreateRoom.window.AddChildWindow(waitDlg.createWaitWnd.window);

	}

	void OnButtonConfirm()
	{
		//创建服务器
		NetManager_I.CreateAsServer();
		
		waitDlg.Show(true);
		waitDlg.createWaitWnd.window.Activate();
	}


	void OnButtonCancel()
	{
		Show(false);
	}

	void Show(bool val)
	{
		WindowCreateRoom.window.SetVisible(val);
	}

	bool isShow()
	{
		return WindowCreateRoom.window.IsVisible();
	}

	Window@				WindowCreateRoom;
	WidgetEditBox@		EditBoxRoomName;
	WidgetEditBox@		EditBoxPassword;
	WidgetButton@		ButtonConfirm;
	WidgetButton@		ButtonCancel;
	CreateWaitDlg@		waitDlg;
}

//视频设置对话框
class VideoSettingDlg : EventHandler
{
	VideoSettingDlg()
	{
		//配置初始化
		OptionsStrInitial();

		//视频设置对话框
		@gameSettingWnd = Window();
		gameSettingWnd.window.LoadLayout("VideoSettings.layout");
		gameSettingWnd.window.SetVisible(false);
		gameSettingWnd.window.SetPos(0.4f,0.15f);

		@videoSetListBox = UiSystem_I.GetListBox("VideoSetting/Frame/Listbox");
		videoSetListBox.changeSelSlot.SetHandler(this,"OnChangeListBoxSel");

		@videoSetCbBox = UiSystem_I.GetComboBox("VideoSetting/Frame/Combobox");
		videoSetCbBox.changeSelSlot.SetHandler(this,"OnChangeComboBoxSel");

		@videoSetBtnOK = UiSystem_I.GetButton("VideoSetting/Frame/ButtonOK");
		videoSetBtnOK.clickSlot.SetHandler(this,"OnOK");

		@videoSetBtnCancel = UiSystem_I.GetButton("VideoSetting/Frame/ButtonCancel");
		videoSetBtnCancel.clickSlot.SetHandler(this,"OnCancel");

		SetListBoxItem();
	}

	void SetListBoxItem()
	{
		for(uint i=0; i<config_I.options.length(); i++)
		{
			videoSetListBox.AddItem(config_I.options[i],i);
		}
	}

	void Show(bool val)
	{
		gameSettingWnd.window.SetVisible(val);
	}

	bool isShow()
	{
		return gameSettingWnd.window.IsVisible();
	}

	//OK按钮回调
	void OnOK()
	{
		//保存配置
		SaveConfig();
		Show(false);
	}

	//Cancel按钮回调
	void OnCancel()
	{
		//配置重新初始化
		OptionsStrInitial();

		Show(false);
	}

	//点击ListBox的更改项回调
	void OnChangeListBoxSel()
	{
		if (videoSetListBox.GetSelectedCount() > 0)
		{
			//选项内容
			string text;
			videoSetListBox.GetCurtSel(text);
			string[] options =  config_I.GetOptionText(text);

			videoSetCbBox.ClearItems();

			int id = config_I.GetOptionId(text);

			videoSetCbBox.SetText(optionsStr[id]);

			for(uint i=0; i<options.length(); i++)
			{
				videoSetCbBox.AddItem(options[i],i);
			}

		}
		else
		{
			videoSetCbBox.ClearItems();
		}
	}

	//点击ComboBox的更改项回调
	void OnChangeComboBoxSel()
	{
		if (videoSetListBox.GetSelectedCount() > 0)
		{
			string sel;
			videoSetCbBox.GetCurtSel(sel);

			string text;
			videoSetListBox.GetCurtSel(text);

			int id = config_I.GetOptionId(text);

			optionsStr[id] = sel;
		}
	}

	// 初始化时使用，把options设置成当前配置
	void OptionsStrInitial()
	{
		optionsStr.resize(7);
		for(int i=0; i<7; i++)
		{
			config_I.GetCurtConfig(i,optionsStr[i]);
		}
	}

	// 更改配置
	void SaveConfig()
	{
		for(uint i=0; i<optionsStr.length(); i++)
		{
			config_I.ChangeConfig(i,optionsStr[i]);
		}
	}

	Window@ gameSettingWnd;

	WidgetListBox@ videoSetListBox;
	WidgetComboBox@ videoSetCbBox;
	WidgetButton@ videoSetBtnOK;
	WidgetButton@ videoSetBtnCancel;

	string[] optionsStr;	//记录更改后的设置
}

//标题界面
class TitleUI : EventHandler
{
	TitleUI()
	{
		//背景窗口
		@window_Title = Window("window_Title");
		//window_Title.window.SetClickEnabled(false);
		window_Title.window.SetProperty("FrameEnabled","false");
		UiSystem_I.CreateImage("title.jpg","image_title");
		window_Title.window.SetImage("image_title");
		UiSystem_I.RootAddWindow(window_Title.window);

		//关于窗口
		@window_About = Window("window_About");
		window_About.window.SetProperty("FrameEnabled","false");
		UiSystem_I.CreateImage("about.jpg","image_about");
		window_About.window.SetImage("image_about");
		window_Title.window.AddChildWindow(window_About.window);
		window_About.window.SetVisible(false);
		window_About.window.SetZOrderingEnabled(true);

		//按钮控件
		@button_CreateRoom = Button("创建房间","button_CreateRoom");
		button_CreateRoom.button.SetCenterPos(0.2f,0.35f);
		button_CreateRoom.button.clickSlot.SetHandler(this,"OnCreateRoom");
		window_Title.window.AddChildWindow(button_CreateRoom.button);
		button_CreateRoom.button.SetZOrderingEnabled(false);

		@button_JoinRoom = Button("加入房间","button_JoinRoom");
		button_JoinRoom.button.SetCenterPos(0.2f,0.4f);
		button_JoinRoom.button.clickSlot.SetHandler(this,"OnJoinRoom");
		window_Title.window.AddChildWindow(button_JoinRoom.button);
		button_JoinRoom.button.SetZOrderingEnabled(false);

		@button_GameOptions = Button("游戏设置","button_GameOptions");
		button_GameOptions.button.SetCenterPos(0.2f,0.45f);
		button_GameOptions.button.clickSlot.SetHandler(this,"OnGameOptions");
		window_Title.window.AddChildWindow(button_GameOptions.button);
		button_GameOptions.button.SetZOrderingEnabled(false);

		@button_About = Button("关于","button_About");
		button_About.button.SetCenterPos(0.2f,0.5f);
		button_About.button.clickSlot.SetHandler(this,"OnAbout");
		window_Title.window.AddChildWindow(button_About.button);
		button_About.button.SetZOrderingEnabled(false);

		@button_ReturnFromAbout = Button("返回","button_ReturnFromAbout");
		button_ReturnFromAbout.button.SetCenterPos(0.65f,0.92f);
		button_ReturnFromAbout.button.clickSlot.SetHandler(this,"OnReturnFromAbout");
		window_About.window.AddChildWindow(button_ReturnFromAbout.button);
		window_About.window.SetZOrderingEnabled(true);

		@videoSetDlg = VideoSettingDlg();
		window_Title.window.AddChildWindow(videoSetDlg.gameSettingWnd.window);

		@createRoomDlg = CreateRoomDlg();
		window_Title.window.AddChildWindow(createRoomDlg.WindowCreateRoom.window);

		@joinRoomDlg = JoinRoomDlg();
		window_Title.window.AddChildWindow(joinRoomDlg.WindowJoinRoom.window);

	}

	void SetVisibleAll(bool val)
	{
		window_Title.window.SetVisible(val);
		button_CreateRoom.button.SetVisible(val);
		button_JoinRoom.button.SetVisible(val);
		button_GameOptions.button.SetVisible(val);
	}

	void HideAll()
	{
		SetVisibleAll(false);
	}


	void OnCreateRoom()
	{
		createRoomDlg.Show(!createRoomDlg.isShow());

	}

	void OnJoinRoom()
	{
		//客户端寻找服务器
		NetManager_I.CreateAsClient();

		joinRoomDlg.RefreshList();
		
		joinRoomDlg.Show(!joinRoomDlg.isShow());

	}

	void OnGameOptions()
	{
		videoSetDlg.Show(!videoSetDlg.isShow());
	}

	void OnAbout()
	{
		window_About.window.SetVisible(true);
		window_About.window.Activate();
	}

	void OnReturnFromAbout()
	{
		window_About.window.SetVisible(false);
	}

	Window@ window_Title;
	Window@ window_About;
	Button@ button_CreateRoom;
	Button@ button_JoinRoom;
	Button@ button_GameOptions;

	Button@ button_About;
	Button@ button_ReturnFromAbout;

	CreateRoomDlg@	 createRoomDlg;
	JoinRoomDlg@  joinRoomDlg;
	VideoSettingDlg@ videoSetDlg;
}

TitleUI@ g_TitleUI;


// <Font Name="BlueHighway-12" Filename="bluehighway-12.font" />
// <Font Name="BlueHighway-10" Filename="bluehighway-10.font" />
// <Font Name="BlueHighway-8" Filename="bluehighway-8.font" />